/* p3_1.c: Initialize and display "Hello" on the LCD using 8-bit data mode. */
/* Data pins use Port B, control pins use Port A */

/* This program strictly follows HD44780 datasheet for timing. You may want to adjust the amount of delay for your LCD controller. */

#include "TM4C123GH6PM.h"

#define LCD_DATA GPIOB 
#define LCD_CTRL GPIOA  
#define RS 0x20 /* PORTA BIT5 mask */
#define RW 0x40 /* PORTA BIT6 mask */
#define EN 0x80 /* PORTA BIT7 mask */

void delayMs(int n);
void delayUs(int n);
void LCD_command(unsigned char command);
void LCD_data(unsigned char data);
void LCD_init(void);

int main(void)
{
    LCD_init();
    for(;;)
    {
        LCD_command(1);       /* clear display */
        LCD_command(0x80);    /* lcd cursor location */
        delayMs(500);
        LCD_data('H');
        LCD_data('e');
        LCD_data('l');
        LCD_data('l');
        LCD_data('o');
        delayMs(500);
    }
}

void LCD_init(void)
{ 
    SYSCTL->RCGCGPIO |= 0x01;  /* enable clock to GPIOA */
    SYSCTL->RCGCGPIO |= 0x02;  /* enable clock to GPIOB */
    
    LCD_CTRL->DIR |= 0xE0;     /* set PORTA pin 7-5 as output for control */
    LCD_CTRL->DEN |= 0xE0;     /* set PORTA pin 7-5 as digital pins */
    LCD_DATA->DIR = 0xFF;      /* set all PORTB pins as output for data */
    LCD_DATA->DEN = 0xFF;      /* set all PORTB pins as digital pins */

    delayMs(20);            /* initialization sequence */
    LCD_command(0x30);
    delayMs(5);
    LCD_command(0x30);
    delayUs(100);
    LCD_command(0x30);
    
    LCD_command(0x38);      /* set 8-bit data, 2-line, 5x7 font */
    LCD_command(0x06);      /* move cursor right */
    LCD_command(0x01);      /* clear screen, move cursor to home */
    LCD_command(0x0F);      /* turn on display, cursor blinking */
}

void LCD_command(unsigned char command)
{
    LCD_CTRL->DATA = 0;     /* RS = 0, R/W = 0 */
    LCD_DATA->DATA = command;
    LCD_CTRL->DATA = EN;    /* pulse E */
    delayUs(0);
    LCD_CTRL->DATA = 0;
    if (command < 4)
        delayMs(2);         /* command 1 and 2 needs up to 1.64ms */
    else
        delayUs(40);        /* all others 40 us */
}

void LCD_data(unsigned char data)
{
    LCD_CTRL->DATA = RS;    /* RS = 1, R/W = 0 */
    LCD_DATA->DATA = data;
    LCD_CTRL->DATA = EN | RS;   /* pulse E */
    delayUs(0);
    LCD_CTRL->DATA = 0;
    delayUs(40);
}

/* delay n milliseconds (16 MHz CPU clock) */
void delayMs(int n)
{
    int i, j;
    for(i = 0 ; i < n; i++)
        for(j = 0; j < 3180; j++)
            {}  /* do nothing for 1 ms */
}

/* delay n microseconds (16 MHz CPU clock) */
void delayUs(int n)
{
    int i, j;
    for(i = 0 ; i < n; i++)
        for(j = 0; j < 3; j++)
            {}  /* do nothing for 1 us */
}

/* This function is called by the startup assembly code to perform system specific initialization tasks. */
void SystemInit(void)
{
    /* Grant coprocessor access */
    /* This is required since TM4C123G has a floating point coprocessor */
    SCB->CPACR |= 0x00f00000;
}